package com.qf.picknamemybaties.servlet;

import com.qf.picknamemybaties.dao.NameDao;
import com.qf.picknamemybaties.entity.Name;
import com.qf.picknamemybaties.utils.MybatiesUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Random;
@WebServlet("/pick")
public class PickNameServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        final NameDao nameDao = MybatiesUtils.getMapper(NameDao.class);
        List<Name> names = nameDao.SelectAllByStatus();
        if(names.size()>0){
            Integer index = new Random().nextInt(names.size());
            Name name = names.get(index);
            resp.getWriter().print(name.getName());
            name.setStatus(1);
            nameDao.updateStatus(name);
            MybatiesUtils.commit();
        }else{
            resp.getWriter().print("点完了");
        }
    }
}
